package cs3343.project.family.UI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import cs3343.project.family.ConfigManager;
import cs3343.project.family.GameController;
import cs3343.project.family.main.SatirTherapy;

/**
 * The Class GameSettingFrame.
 */
public class GameSettingFrame extends JFrame implements ActionListener {
	
	/** The gc. */
	private GameController gc;

	/** The content pane. */
	private JPanel contentPane;
	
	/** The txt win. */
	private JTextField txtWin;
	
	/** The txt lost. */
	private JTextField txtLost;
	
	/** The cbx_lang choice. */
	private JComboBox<String> cbx_langChoice;
	
	/** The lbl win. */
	private JLabel lblWin;
	
	/** The lbl lost. */
	private JLabel lblLost;
	
	/** The btn save. */
	private JButton btnSave;
	
	/** The btn reset to default. */
	private JButton btnResetToDefault;
	
	/** The btn return. */
	private JButton btnReturn;

	/**
	 * Create the frame.
	 *
	 * @param gc the gc
	 */
	public GameSettingFrame(GameController gc) {
		ConfigManager.readConfig();
		
		this.gc = gc;
		
		initUI();
		
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		JLabel lblLanguage = new JLabel(ConfigManager.getLangString("lblLanguage"));
		GridBagConstraints gbc_lblLanguage = new GridBagConstraints();
		gbc_lblLanguage.insets = new Insets(0, 0, 5, 5);
		gbc_lblLanguage.gridx = 1;
		gbc_lblLanguage.gridy = 1;
		contentPane.add(lblLanguage, gbc_lblLanguage);
		
		cbx_langChoice = new JComboBox(ConfigManager.LANG[0]);
		cbx_langChoice.setSelectedIndex(ConfigManager.getLang_index());
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.insets = new Insets(0, 0, 5, 0);
		gbc_comboBox.gridx = 3;
		gbc_comboBox.gridy = 1;
		contentPane.add(cbx_langChoice, gbc_comboBox);
		
		lblWin = new JLabel(ConfigManager.getLangString("lblWin"));
		GridBagConstraints gbc_lblWin = new GridBagConstraints();
		gbc_lblWin.insets = new Insets(0, 0, 5, 5);
		gbc_lblWin.gridx = 1;
		gbc_lblWin.gridy = 3;
		contentPane.add(lblWin, gbc_lblWin);
		
		txtWin = new JTextField();
		txtWin.setText(String.valueOf(ConfigManager.getWin_esteem()));
		GridBagConstraints gbc_txtWin = new GridBagConstraints();
		gbc_txtWin.insets = new Insets(0, 0, 5, 0);
		gbc_txtWin.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtWin.gridx = 3;
		gbc_txtWin.gridy = 3;
		contentPane.add(txtWin, gbc_txtWin);
		txtWin.setColumns(10);
		
		lblLost = new JLabel(ConfigManager.getLangString("lblLost"));
		GridBagConstraints gbc_lblLost = new GridBagConstraints();
		gbc_lblLost.insets = new Insets(0, 0, 5, 5);
		gbc_lblLost.gridx = 1;
		gbc_lblLost.gridy = 5;
		contentPane.add(lblLost, gbc_lblLost);
		
		txtLost = new JTextField();
		txtLost.setText(String.valueOf(ConfigManager.getLost_esteem()));
		GridBagConstraints gbc_txtLost = new GridBagConstraints();
		gbc_txtLost.insets = new Insets(0, 0, 5, 0);
		gbc_txtLost.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtLost.gridx = 3;
		gbc_txtLost.gridy = 5;
		contentPane.add(txtLost, gbc_txtLost);
		txtLost.setColumns(10);
		
		btnSave = new JButton(ConfigManager.getLangString("btnSave"));
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.insets = new Insets(0, 0, 5, 0);
		gbc_btnSave.gridx = 3;
		gbc_btnSave.gridy = 7;
		contentPane.add(btnSave, gbc_btnSave);
		
		btnResetToDefault = new JButton(ConfigManager.getLangString("btnResetToDefault"));
		GridBagConstraints gbc_btnResetToDefault = new GridBagConstraints();
		gbc_btnResetToDefault.gridx = 3;
		gbc_btnResetToDefault.gridy = 8;
		contentPane.add(btnResetToDefault, gbc_btnResetToDefault);
		
		btnReturn = new JButton(ConfigManager.getLangString("btnReturn"));
		GridBagConstraints gbc_btnReturn = new GridBagConstraints();
		gbc_btnReturn.gridx = 3;
		gbc_btnReturn.gridy = 9;
		contentPane.add(btnReturn, gbc_btnReturn);
		
		btnSave.addActionListener(this);
		btnResetToDefault.addActionListener(this);
		btnReturn.addActionListener(this);
		
		gc.getUiController().frameCentering(this);
	}
	
	/**
	 * Inits the ui.
	 */
	public void initUI() {
		setTitle(ConfigManager.getLangString("gameSettingFrameTitle"));
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
	}
	
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnSave) {
			 if (ConfigManager.saveConfig(String.valueOf(cbx_langChoice.getSelectedIndex()), txtWin.getText(), txtLost.getText())) {
				 JOptionPane.showMessageDialog(null, ConfigManager.getLangString("msgUpdated"));
				 ConfigManager.loadLangFile();
				 gc.getUiController().showWelcomeFrame();
				 this.dispose();
			 }
			 else {
				 JOptionPane.showMessageDialog(null, 
						 ConfigManager.getLangString("msgFailUpdate"),
						 ConfigManager.getLangString("errorTitle"),
						 JOptionPane.ERROR_MESSAGE);
				 gc.getUiController().showWelcomeFrame();
				 this.dispose();
			 }
		}
		else if (e.getSource() == btnResetToDefault) {
			if (ConfigManager.restoreDefault()) {
				 JOptionPane.showMessageDialog(null, ConfigManager.getLangString("msgReset"));
			}
			else {
				 JOptionPane.showMessageDialog(null,
						 ConfigManager.getLangString("msgFailReset"),
						 ConfigManager.getLangString("errorTitle"),
						 JOptionPane.ERROR_MESSAGE);
			}
		}
		else if (e.getSource() == btnReturn) {
			SatirTherapy.main(null);
			this.dispose();
		}
	}
}
